﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Interactivity;
using System.Windows.Controls;

namespace WpfWordsLearner
{
	public class WatermarkTextBoxBehavior : Behavior<TextBox>
	{
		public string Watermark
		{
			get { return (string)GetValue(WatermarkProperty); }
			set { SetValue(WatermarkProperty, value); }
		}

		// Using a DependencyProperty as the backing store for Watermark.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty WatermarkProperty =
			DependencyProperty.Register("Watermark", typeof(string), typeof(WatermarkTextBoxBehavior), new UIPropertyMetadata(string.Empty));


		protected override void OnAttached()
		{
			base.OnAttached();
			this.AssociatedObject.GotFocus += new RoutedEventHandler(AssociatedObject_GotFocus);
			AssociatedObject.LostFocus += new RoutedEventHandler(AssociatedObject_LostFocus);
			AssociatedObject.TextChanged += new TextChangedEventHandler(AssociatedObject_TextChanged);
		}

		protected override void OnDetaching()
		{
			base.OnDetaching();
			this.AssociatedObject.GotFocus -= new RoutedEventHandler(AssociatedObject_GotFocus);
			AssociatedObject.LostFocus -= new RoutedEventHandler(AssociatedObject_LostFocus);
			AssociatedObject.TextChanged -= new TextChangedEventHandler(AssociatedObject_TextChanged);
		}

		void AssociatedObject_LostFocus(object sender, RoutedEventArgs e)
		{
			if (string.IsNullOrEmpty(AssociatedObject.Text))
			{
				this.AssociatedObject.TextChanged -= new TextChangedEventHandler(AssociatedObject_TextChanged);
				AssociatedObject.Foreground = System.Windows.Media.Brushes.Silver;
				AssociatedObject.Text = Watermark;
				this.AssociatedObject.TextChanged += new TextChangedEventHandler(AssociatedObject_TextChanged);
				e.Handled = true;
			}
		}

		void AssociatedObject_GotFocus(object sender, RoutedEventArgs e)
		{
			if (AssociatedObject.Text.Length == 0 && !string.IsNullOrEmpty(Watermark))
			{
				this.AssociatedObject.TextChanged -= new TextChangedEventHandler(AssociatedObject_TextChanged);
				AssociatedObject.Foreground = System.Windows.Media.Brushes.Silver;
				AssociatedObject.Text = Watermark;
				this.AssociatedObject.TextChanged += new TextChangedEventHandler(AssociatedObject_TextChanged);
				e.Handled = true;
			}
		}

		void AssociatedObject_TextChanged(object sender, TextChangedEventArgs e)
		{
			if (AssociatedObject.Text.Length == 0 && !string.IsNullOrEmpty(Watermark))
			{
				this.AssociatedObject.TextChanged -= new TextChangedEventHandler(AssociatedObject_TextChanged);
				AssociatedObject.Foreground = System.Windows.Media.Brushes.Silver;
				AssociatedObject.Text = Watermark;
				this.AssociatedObject.TextChanged += new TextChangedEventHandler(AssociatedObject_TextChanged);
				e.Handled = true;
			}
			else if (AssociatedObject.Text.IndexOf(Watermark) > 0)
			{
				this.AssociatedObject.TextChanged -= new TextChangedEventHandler(AssociatedObject_TextChanged);
				AssociatedObject.Text = AssociatedObject.Text.Remove(AssociatedObject.Text.IndexOf(Watermark));
				AssociatedObject.Foreground = System.Windows.Media.Brushes.Black;
				this.AssociatedObject.TextChanged += new TextChangedEventHandler(AssociatedObject_TextChanged);
				e.Handled = true;
			}
		}

	}
}
